function ValidateForm(validateFormOpt) {

    //初始化输入字段配置
    this._initValiFields = function () {
        this.fieldsOpt = validateFormOpt.fieldsOpt
    }
    //根据slector找到对应的输入字段元素

    this._getIpts = function () {
        //输入框元素
        this.iptEle = {}
        for (let key in this.fieldsOpt) {
            this.iptEle[key] = document.querySelector(this.fieldsOpt[key].selector)
        }
    }

    //初始化验证结果对象
    this._initValiRes = function () {
        //验证结果
        let valiResObj = {}
        for (let field in this.fieldsOpt) {
            valiResObj[field] = false
        }
        //验证结果对象添加pass属性，
        Object.defineProperty(valiResObj, 'pass', {
            get() {
                let res = true
                for (let key in this) {
                    res = res && this[key]
                    if (!res) { break }
                }
                return res
            }
        })
        this.valiRes = valiResObj
    }
    //给所有字段绑定失去焦点事件
    this.bindFieldEvent = function () {
        for (let field in this.fieldsOpt) {
            //更具字段找到当前验证的元素
            let curIpt = this.iptEle[field]
            let _this = this
            // console.log(this)
            curIpt.onblur = function () {
                // console.log(this)
                //this指向当前元素
                let value = this.value
                _this.valiRes[field] = _this._valiField(field, value)
            }
        }
    }

    //验证一个字段
    this._valiField = function (field, value) {
        let valiRes = true
        console.log(`验证${field}`)
        //验证该字段的所有规则
        let ruleObj = this.fieldsOpt[field].rule
        for(let rule in ruleObj){
            console.log(`验证${field}的${rule}`)
            let ruleRes =  eval(`this._vali_${rule}(ruleObj,value)`)
            if(!ruleRes){
                valiRes = ruleRes
                break
            }
        }
        this._handleFieldRes(field, valiRes)
        return valiRes
    }
    /*******************验证方式******************/
    //正则验证
    this._vali_reg = function(ruleObj,value){
        return ruleObj.reg.test(value)
    }
    //值验证
    this._vali_value = function(ruleObj,value){
        return value == ruleObj.value
    }
    //字段值相等验证
    this._vali_equal = function(ruleObj,value){
        let targetIpt = this.iptEle[ruleObj.equal]
        return targetIpt.value == value
    }
   
    //处理字段验证结果
    this._handleFieldRes = function (field, valiRes) {
        console.log('字段验证结果')
        try { 
            validateFormOpt.valiField(field, valiRes)
        }catch(e){
            console.error(e)
        }
    },

    this._handleSubmit = function (validateFormOpt) {
        let submit = document.querySelector(validateFormOpt.submit.selector)
        let _this = this
        submit.addEventListener('click', function (e) {
            if (!_this.isPass()) {
                validateFormOpt.error(e)
            } else {
                validateFormOpt.success(e)
            }

        })
    }

    //让元素获得焦点
    this._focus = function (field) {
        this.iptEle[field].focus()
    }

    //表单所有字段验证结果
    this.isPass = function () {
        return this.valiRes.pass
    }

    //开启实例表单验证
    this.validate = function () {
        this.bindFieldEvent()
        this._handleSubmit(validateFormOpt)
    }


    /********************调用函数,实现数据初始化*****************************/
    //初始化字段配置
    this._initValiFields()
    //初始化字段对应的元素
    this._getIpts()
    //初始化字段验证结果
    this._initValiRes()

}